package com.zx.controller.files;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.print.attribute.standard.MediaTray;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.UUID;

/**
 * @author sunkai
 * @date 2022/8/16 0016 21:09
 * @mood zhou xun
 */
@Controller
public class UploadController {

    @PostMapping(value = "/mvc/upload")
    @ResponseBody
    public String upload(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {
        //上传路径
        String path="C:\\images";
        File realPath = new File(path);
            if (!realPath.exists()){
                realPath.mkdir();
            }
        //获取到上传文件的名称
        String filename = file.getOriginalFilename();
        //获取文件后缀
        String exName = StringUtils.getFilenameExtension(filename);
        String newFileName = UUID.randomUUID().toString()+ "." + exName;
        //上传文件
        file.transferTo(new File(path, newFileName));
        return newFileName;
    }
}
